# count.S
#
# - Argumentos:
#	* fd: int
#
# - Salida: int
#
# - Stack Frame:
#	* Tamaño: 8 bytes (por ser leaf, guardo gp y fp)

#define 	SSIZE		8
#define		O_FP		4
#define		O_GP		0
#define		O_ARG0		(SSIZE)
#define		O_ARG1		SSIZE + 4

#define	ASCII_SPACE	32
#define	ASCII_HTAB	9
#define	ASCII_NEWLINE	10
#define ASCII_CRETURN	13
#define ASCII_VTAB	11
#define ASCII_FFEED	12

#include <mips/regdef.h>
#include <sys/syscall.h>

#int count(char c, int last_char)
#{
#	int ret = 1;
#	switch (c)
#	{
#		case ' ':
#		case '\t':
#		case '\r':
#		case '\f':
#		case '\v'
#			break;
#		case '\n':		
#			++lines;
#			break;
#		default:
##			if (last_char)
#				++words;
#			ret = 0;
#			break;
#	}
#	++bytes;
#	return ret;
#}

.text
.align 2
.globl 	count
.ent 	count

count:			
	.frame $fp, SSIZE, ra
	.set noreorder
	.cpload t9
	.set reorder


			subu	sp, sp, SSIZE			#creo el stack
			sw	$fp, O_FP(sp)
			.cprestore O_GP		
			
			move 	$fp, sp
			sb 	a0, O_ARG0($fp)
			sw	a1, O_ARG1($fp)
		
			move	t0, zero
			lb	t0, O_ARG0($fp)
			
			li	v0, 1
			beq	t0, ASCII_SPACE, fin			
			beq	t0, ASCII_HTAB, fin
			beq	t0, ASCII_CRETURN, fin
			beq	t0, ASCII_FFEED, fin
			beq	t0, ASCII_VTAB, fin
			bne	t0, ASCII_NEWLINE, default
			lw	t1, lines
			addiu	t1, t1, 1
			sw	t1, lines
			b	fin
default:		li	v0, 0
			beqz	a1, fin
			lw	t1, words
			addiu	t1, t1, 1
			sw	t1, words
			
fin:			lw	t1, bytes
			addiu	t1, t1, 1
			sw	t1, bytes
		
			move 	sp, $fp					#destruyo el stack
			lw	gp, O_GP($fp)
			lw	$fp, O_FP($fp)
			addiu 	sp, sp, SSIZE
			jr 	ra
.end 	count


	
